﻿@using ABC.Controllers.ViewModels
@using ABC.Controllers.ViewModels.My
@using ABC.Domain.Entities
@model MyClientsViewModel
@{
    ViewBag.Title = "Мои клиенты - Азбука24";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
    var query = Request.Params["query"];
    var status = Request.Params["status"];
    var pagesize = Request.Params["pagesize"];
}
<script>
    function ChangePageSize() {
        if ('@Request.QueryString.AllKeys.Length' == '1' && '@Request.QueryString.AllKeys.Contains("pagesize")' == 'True') {
            location.href = '@Url.Action("Index", "Clients")' + "?pagesize=" + $("select[name='PageSize']").val();
            return;
        }
        if ('@Request.QueryString.AllKeys.Length' == '0') {
            location.href = '@Url.Action("Index", "Clients")' + "?pagesize=" + $("select[name='PageSize']").val();
            return;
        }
        location.href = '@Url.Action("Finance", "Admin")' + "?q=" + "@query" + "&status=" + "@status" + "&pagesize=" + $("select[name='PageSize']").val();
    }
    function Call(obj) {
        $(".preview-phone a").attr("href", "callto:" + $(obj).attr("phone"));
        $(".preview-phone h1").text($(obj).attr("phone"));
        $("#call-window .modal-header h3").text($(obj).parents("tr").children("td").eq(1).children("ul").children("li").children("a").text());
        $("#call-window").modal("show");
    }
    function ResetFilter() {
        $("#Query").val("");
        $("#Status").val("");
        location.href = '@Url.Action("Index", "Clients")';
    }
    function GetClients() {
        location.href = '@Url.Action("Index", "Clients")' + "?query=" + $("#Query").val() + "&status=" + $("#Status").val() + "&pagesize=" + $("select[name='PageSize']").val();
    }
</script>
<div class="row-fluid">
    <!--// Левая колонка -->
    @{Html.RenderPartial("_SectionClientsMenu", new BaseViewModel());}
    <!--// Правая колонка -->
    <div class="span9">
        <div class="page-header">
            <h2>
                Мои клиенты</h2>
        </div>
        <div class="row-fluid">
            <div class="span3">
                <div class="control-group">
                    <label class="control-label" for="Query">
                        Поиск</label>
                    <div class="controls">
                        @Html.TextBoxFor(x => x.Query, new { @class = "search-query input-medium", placeholder = "Данные клиента" })
                    </div>
                </div>
            </div>
            <div class="span3">
                <div class="control-group">
                    <label class="control-label" for="Status">
                        Статус</label>
                    <div class="controls">
                        @Html.DropDownListFor(x => x.Status, Model.ListForStatus, new { @class = "input-medium" })
                    </div>
                </div>
            </div>
            <div class="span3">
            </div>
            <div class="span3">
                <div class="control-group">
                    <label class="control-label" for="phone-01">
                        Фильтр</label>
                    <div class="controls">
                        <div class="btn-group">
                            <button type="button" class="btn" onclick="GetClients();">
                                Применить</button>
                            <button class="btn" rel="tooltip" onclick="ResetFilter();" data-original-title="Сбросить фильтр">
                                <i class="icon-refresh"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th class="col-1">
                        <input type="checkbox" id="inlineCheckbox01" onchange="ChangeAllCheck();" value="option0"
                            title="Выделить все">
                    </th>
                    <th>
                        Имя или название
                    </th>
                    <th>
                        Объект страхования
                    </th>
                    <th>
                        Телефон
                    </th>
                    <th>
                        Электронная почта
                    </th>
                </tr>
            </thead>
            <tbody>
                @using (Html.BeginForm("DeleteClients", "Clients", FormMethod.Post, new { id = "form1" }))
                {
                    @Html.Hidden("query")
                    @Html.Hidden("status")
                    @Html.Hidden("pagesize", Model.PageSize == 0 ? "" : Model.PageSize.ToString())
                    @Html.Hidden("page")
                    foreach (var client in Model.Clients)
                    {
                    <tr>
                        <td class="col-1">
                            <input type="checkbox" onchange="ChangeCheck(this);" name="clientid" value="@client.Id">
                        </td>
                        <td>
                            <ul class="unstyled">
                                <li><a href="@Url.Action("Client", new { id = client.Id })">@(string.Format("{0} {1} {2}",client.LastName,client.FirstName, client.MiddleName))</a></li>
                            </ul>
                        </td>
                        <td>
                            <ul class="unstyled">
                                @if (client.ObjectsOfInsurance.Count == 0)
                                {
                                    <li><a class="add-object-link" title="Создать объект страхования" href="@Url.Action("AddObject", "Cabinet", new { insurerId = client.Id })">
                                        Объекты страхования отсутствуют</a></li>
                                }
                                @foreach (var objectOfInsurance in client.ObjectsOfInsurance)
                                {
                                    <li><a href="@Url.Action("AddObject", "Cabinet", new { id = objectOfInsurance.Id })">@(string.Format("{0} {1}, {2} ", objectOfInsurance.Manufacture, objectOfInsurance.Model, objectOfInsurance.Release))</a></li>
                                }
                            </ul>
                        </td>
                        <td>
                            <ul class="unstyled">
                                @foreach (var phone in client.Phones)
                                {
                                    <li><a href="javascript:void(0)" onclick="Call(this);" phone="@phone.Value">@phone.Value</a></li>
                                }
                            </ul>
                        </td>
                        <td>
                            <ul class="unstyled">
                                @foreach (var email in client.Emails)
                                {
                                    <li><a href="mailto:@email.Value">@email.Value</a></li>
                                }
                            </ul>
                        </td>
                    </tr>
                    }
                }
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="100%">
                        <div class="table-toolbar">
                            <div class="quant-item pull-left">
                                @Html.DropDownListFor(x => x.PageSize, Model.ListForPageSize, new { @class = "input-mini", onchange = "ChangePageSize();" })
                                <span class="help-inline">записей на странице</span>
                            </div>
                            <div class="pagination pagination-right">
                                <ul>
                                    @Html.Paging(Model.PageNumber, Model.PageCount, i => Url.Action("Index", "Clients", new { page = i, query, status, pagesize }))
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
        <div class="btn-toolbar">
            <button class="btn btn-primary" onclick="location.href='@Url.Action("Add", "Clients", new { id = "" })'">
                Создать клиента</button>
            <button class="btn" disabled="disabled" onclick="$('#form1').submit();" name="delete" state="watch">
                Удалить</button>
        </div>
    </div>
</div>
<script type="text/javascript">
    allCheckboxes = $("tbody .col-1 input[type=checkbox]");
</script>
<div class="modal hide " id="call-window">
    <div class="modal-header">
        <button data-dismiss="modal" class="close">
            ×</button>
        <h3>
        </h3>
    </div>
    <div class="modal-body">
        <div class="preview-phone">
            <h1>
            </h1>
            <a href="" class="btn btn-success"><i class="icon-phone"></i>Позвонить</a>
        </div>
    </div>
    <div class="modal-footer">
        <!-- пока эту кнопку убираем// button class="btn btn-primary">Позвонить с сайта</button -->
        <button data-dismiss="modal" class="btn">
            Закрыть окно</button>
    </div>
</div>
